import { h, ref, onMounted } from "vue";
/** 新增按钮 */
import { addDialog } from "@/components/ReDialog";
import Form from "../form/index.vue";
import { getRewardRecord } from "@/api/reward";

export function useDeliver() {
  // ⚙️状态初始化
  const formRef = ref();
  /** 重置密码 */
  /** 是否处于 Loding 状态 */
  const loading = ref(true);
  /** 表格数据 */
  const dataList = ref([]);
  /** 定义表头 */
  const columns: TableColumnList = [
    {
      label: "勾选列", // 如果需要表格多选，此处label必须设置
      type: "selection",
      fixed: "left",
      reserveSelection: true // 数据刷新后保留选项
    },
    {
      label: "创建时间",
      prop: "creadetime",
      minWidth: 130
    },
    {
      label: "用户名",
      prop: "consumername",
      minWidth: 130
    },
    {
      label: "商品名",
      prop: "snapshot.name",
      minWidth: 130
    },
    {
      label: "兑换数量",
      prop: "sumnum",
      minWidth: 130
    },
    {
      label: "兑换积分",
      prop: "pointconsume",
      minWidth: 130
    },
    {
      label: "备注信息",
      prop: "note",
      minWidth: 130
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];

  // 打开地址详情窗口
  const openDialog = async (title = "查看收货信息", row?: any) => {
    addDialog({
      title: title,
      props: {
        formInline: {
          title,
          recipientsaddr: row?.recipientsaddr ?? "",
          recipientsname: row?.recipientsname ?? "",
          recipientsphone: row?.recipientsphone ?? "",
          note: row?.note ?? ""
        }
      },
      width: "50%",
      // contentRenderer: () => h(Form, { ref: formRef, formInline: row }),
      contentRenderer: () => h(Form, { ref: formRef })
    });
  };

  const onSelectionCancel = async (row: any) => {
    console.log(row);
    // const res = await deleteReward({ goodsid: row.goodsid });
    // if (res.code === 200) {
    //   message(res.msg);
    //   await onSearch();
    // }
  };
  const onbatchDel = async (row: any) => {
    console.log(row);
    // const res = await deleteReward({ goodsid: row.goodsid });
    // if (res.code === 200) {
    //   message(res.msg);
    //   await onSearch();
    // }
  };

  /** 默认搜索方法 */
  async function onSearch() {
    loading.value = true;
    const { data } = await getRewardRecord();
    dataList.value = data;
    // pagination.total = result.total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  /** 页面挂载钩子 */
  onMounted(async () => {
    onSearch();
  });

  return {
    formRef,
    loading,
    columns,
    dataList,
    onSearch,
    openDialog,
    onSelectionCancel,
    onbatchDel
  };
}
